home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: usenet.eel.ufl.edu!warwick!bsmail!talisker!nathan
- From: nathan@pact.srf.ac.uk (Nathan Sidwell)
- Subject: Re: Help with pointer alignment
- Message-ID: <DMI7Hz.B8C@uns.bris.ac.uk>
- Sender: usenet@uns.bris.ac.uk (Usenet news owner)
- Nntp-Posting-Host: talisker.pact.srf.ac.uk
- Organization: Inmos
- X-Newsreader: TIN [version 1.2 PL2]
- References: <4fb0op$8l8@hermes.louisville.edu>
- Date: Fri, 9 Feb 1996 10:22:47 GMT
-
- Alan Wild (arwild01@homer.louisville.edu) wrote:
- : I am having a small problem with pointer alignment in a library I am
- : developing. lint reports:
-
- : "queue.c", line 13: warning: possible pointer alignment problem, op CAST
-
- : And the code is as follows:
-
- : struct dataChain {
- : ORD64 dataf;
- : ORD64 be;
- : int cycle;
- : struct dataChain *next;
- : };
-
- : typedef struct dataChain queueentry;
-
- : void Append ( queueentry new, queue *Q ) {
-
- : queueentry *temp = (queueentry *)malloc( sizeof( queueentry ) );
-
-
- : The line in error is the malloc statement. What have I done
- : wrong/dangerously? I would like to correct this if at all possible.
- : This is the only pententially problematic warning in my code and I would
- : like to avoid any problems. :)
-
- assuming that malloc has been declared, the compiler sees that
- malloc returns a (char *) or a (void *).
-
- A char * has
- less strict alignment restrictions than a queueentry * (on your system),
- and the compiler sees that your casting the loose pointer to the tight
- pointer, possibly causing a problem. The compiler doesn't appear to
- know that malloc always returns a correctly aligned pointer.
-
- If malloc is declared to return a void *, I think the compiler's
- warning unnecessarily, as it should know nothing about the alignment
- of the void *.
-
- Your code is fine, your compiler is warning unnecessarily. See if
- writing it as (queueentry *)(void *)malloc(...) shuts it up.
-
- (I've just noticed your talking about lint, not a compiler, but the above
- argument still holds, but perhaps it's not possible to stop lint telling
- you this.)
- nathan
-
- --
- Nathan Sidwell Holder of the Xmris home page
- Chameleon Architecture Group at SGS-Thomson, formerly Inmos
- http://www.pact.srf.ac.uk/~nathan/ Tel 0117 9707182
- nathan@inmos.co.uk or nathan@bristol.st.com or nathan@pact.srf.ac.uk
-